//
//  BBContainerInfoView.m
//  xNose
//
//  Created by ben smith on 9/8/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BBContainerInfoView.h"
#import "BBNode.h"
#import "BBDefaultController.h"
#import "BBNodeInfoView.h"
#import "BBSubtotalNodeView.h"
#import "BBTotalsValueView.h"

@implementation BBContainerInfoView

@synthesize node;

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
//			self.wantsLayer = YES;
			verticalOffset = 0;
    }
    return self;
}

-(void)setupViews
{
	// need to add a holder
	NSRect bigTall = [self bounds];
	bigTall.origin.y -= 3000;
	bigTall.size.height += 3000;
	viewHolder = [[NSView alloc] initWithFrame:bigTall];
	[self addSubview:viewHolder];
	
	// now add my views to the holder view
	float textHeight = 100;
	NSRect textblock = NSMakeRect(0, NSHeight(bigTall) - textHeight, NSWidth(bigTall), textHeight);
	BBNodeInfoView* nodeInfo = [[BBNodeInfoView alloc] initWithFrame:textblock];
	[nodeInfo setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable | NSViewMinXMargin | NSViewMaxXMargin |NSViewMinYMargin | NSViewMaxYMargin];
	[nodeInfo setNode:node];
	[viewHolder addSubview:nodeInfo];
	
	float currentY = NSMinY(textblock);
	
	for (BBNode* collection in node.children) {
		float height = [self infoHeightForNode:collection];
		NSRect subtotaltest = NSMakeRect(0, currentY - height, NSWidth([self frame]), height);
		BBSubtotalNodeView * subView = [[BBSubtotalNodeView alloc] initWithFrame:subtotaltest];
		[subView setNode:collection];
		[viewHolder addSubview:subView];
		[subView release];
		currentY -= height + 20;
	}
	
	currentY -= 90;
	NSRect totaltest = NSMakeRect(0, currentY, NSWidth([self frame]), 90);
	BBTotalsValueView * tView = [[BBTotalsValueView alloc] initWithFrame:totaltest];
	[tView setNode:self.node];
	[viewHolder addSubview:tView];		
	[tView release];
}

-(float)infoHeightForNode:(BBNode*)aNode
{
	// hacky! hardcoded!!
	if ([aNode isLeaf]) return 75.0;
	if ([aNode isCollection]) {
		float h = 85.0 + 20.0; // room for totals plus the thing at the top
		if ([aNode hasChildren]) {
			h += [aNode.children count] * 85.0;
		}
		return h;
	}
	return 0;
}

-(void)setNode:(BBNode*)aNode
{
	[aNode retain];
	[node release];
	node = aNode;
	
	[self setupViews];
}


-(void)otherMouseDown:(NSEvent*)theEvent
{
	[self mouseDown:theEvent];
}

-(void)otherMouseUp:(NSEvent*)theEvent
{
	[self mouseUp:theEvent];
}

-(void)otherMouseDragged:(NSEvent*)theEvent
{
	[self mouseDragged:theEvent];
}


// suck up extra mouse events
-(void)mouseDown:(NSEvent*)theEvent
{
	mouseDown = [theEvent locationInWindow];	
}

-(void)mouseUp:(NSEvent*)theEvent
{
	
}

// scroll the libview
-(void)mouseDragged:(NSEvent*)theEvent
{
	NSPoint newMouse = [theEvent locationInWindow];
	verticalOffset -= newMouse.y - mouseDown.y;
	[self shiftBy:-(newMouse.y - mouseDown.y)];
	mouseDown = newMouse;
}

-(void)shiftBy:(float)adjust
{
	NSRect f = [viewHolder frame];
	f.origin.y -= adjust;		
	[viewHolder setFrame:f];
	[self setNeedsDisplay:YES];	
}

@end
